<template>
	<view>
		<view class="commentFrame">
			<uni-easyinput ref="uipt" @confirm="goComment" suffixIcon="paperplane" v-model="replyContent" :placeholder="placeholder" @iconClick="goComment"></uni-easyinput>
		</view>
	</view>
</template>

<script>
import { getProvince } from '../../utils/tools.js';
const db = uniCloud.database();
// 引入云对象
const utilsObj = uniCloud.importObject('utilsObj', {
	// 移除自带loadding
	customUI: true
});
export default {
	name: 'comment-frame',
	props: {
		commentObj: {
			type: Object,
			default: () => {
				return {};
			}
		},
		placeholder:{
			type:String,
			default:"评论点为什么吧～"
		}
	},
	data() {
		return {
			replyContent: '',
			cityStr: '',
			cityCode: ''
		};
	},
	onLoad() {},
	methods: {
		async goComment() {
			if (!this.replyContent) {
				uni.showToast({
					title: '内容不能为空'
				});
				return;
			}
			// 获取城市信息
			let cityData = await getProvince();
			//发布评论
			let res = await db.collection('quanzi-comment').add({
				comment_content: this.replyContent,
				cityStr: cityData.cityStr,
				cityCode: cityData.cityCode,
				...this.commentObj
			});
			uni.showToast({
				title: '评论成功'
			});
			console.log(res);
			// 子传父 实现无感刷新
			this.$emit('commentChange', {
				_id: res.result._id,
				comment_content: this.replyContent,
				cityStr: cityData.cityStr,
				cityCode: cityData.cityCode,
				comment_date: Date.now()
			});
			// 清空输入框
			this.replyContent = '';
			// 增加评论数量
			utilsObj.operation('quanzi_artcle', 'comment_count', this.commentObj.article_id, 1);
		}
	}
};
</script>

<style lang="scss" scoped>
.commentFrame {
	width: 100%;
	background: #fff;
	padding: 20rpx 30rpx;
	box-sizing: border-box;
	position: fixed;
	bottom: 0;
	left: 0;
	z-index: 100;
}
</style>
